package sfeir.gwt.ergosum.client.profile;

import java.util.ArrayList;
import java.util.List;

import sfeir.gwt.ergosum.client.ClientApi;
import sfeir.gwt.ergosum.client.ClientApiAsync;
import sfeir.gwt.ergosum.client.ClientApi.NotLoggedInException;
import sfeir.gwt.ergosum.client.model.Address;
import sfeir.gwt.ergosum.client.model.Email;
import sfeir.gwt.ergosum.client.model.GeoLocalisation;
import sfeir.gwt.ergosum.client.model.ImageURL;
import sfeir.gwt.ergosum.client.model.NetworkItem;
import sfeir.gwt.ergosum.client.model.Person;
import sfeir.gwt.ergosum.client.model.Tel;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class ViewProfileEntryPoint implements EntryPoint, AsyncCallback<Person> {

    @Override
    public void onModuleLoad() {
        String email = getUsername();
        ClientApiAsync server = (ClientApiAsync) GWT.create(ClientApi.class);
        if (email == null)
            server.getUserProfile(this);
        else
            server.getProfile(email, this);
    }

    
    protected Person testPerson(String username) {
        Person person = new Person();
        person.setFirstname("Patrice");
        person.setName("De saint Steban");
        person.setPhoto(new ImageURL("http://www.viadeo.com/servlet/photo?memberId=002h0a0aplk6spy&ts=946681200000&type=0"));
        person.setAboutMe("About me ....");
        person.setBirthDay("03/03/1986");
        person.setCompany("SFEIR");
        person.setGeo(new GeoLocalisation(48.9, 2.07));
        List<NetworkItem> networks = new ArrayList<NetworkItem>();
        networks.add(new NetworkItem("facebook", "Facecbook.com"));
        networks.add(new NetworkItem("twitter", "twitter.com"));
        networks.add(new NetworkItem("viadeo", "viadeo.com"));
        networks.add(new NetworkItem("skype", "skype.com"));
        networks.add(new NetworkItem("vimeo", "vimeo.com"));
        networks.add(new NetworkItem("dopplr", "dopplr.com"));
        networks.add(new NetworkItem("lastfm", "lastFM.com"));
        networks.add(new NetworkItem("readernaut", "readernaut.com"));
        networks.add(new NetworkItem("friendfeed", "Friendfeed.com"));
        networks.add(new NetworkItem("web", "site.com"));
        person.setNetworkItems(networks);
        List<Address> address = new ArrayList<Address>();
        address.add(new Address("60 rue Camille desmoulins 94800 VILLEJUIF", Address.DEFAULT_ADDRESS));
        address.add(new Address("Le gué briand 35720 TRESSE", Address.PERSONAL_ADDRESS));
        address.add(new Address("2 rue des bourets Suresnes", Address.PROFESSIONNAL_ADDRESS));
        person.setAddresses(address);
        List<Tel> tel = new ArrayList<Tel>();
        tel.add(new Tel("0953868603", Tel.HOME_LANDLINE));
        tel.add(new Tel("0673116302", Tel.MOBILE_LINE));
        tel.add(new Tel("0299737423", Tel.PROFESSIONNAL_LINE));
        person.setTels(tel);
        List<Email> email = new ArrayList<Email>();
        email.add(new Email("de-sai_p@epita.fr", Email.PERSONAL_EMAIL));
        email.add(new Email("desaintsteban.p@sfeir.com", Email.PROFESSIONNAL_EMAIL));
        email.add(new Email("patou.de.saint.steban@gmail.com", Email.UNKNOWN));
        person.setEmails(email);
        return person;        
    }
    
    @Override
    public void onSuccess(Person result) {
        RootPanel.get("content").getElement().setInnerText("");
        if (result != null) {
            RootPanel.get("content").add(new ViewProfile(result));
        }
        else {
            RootPanel.get("content").add(new Label("The profile of this contact didn't exist, create your profile"));
        }
    }
    
    @Override
    public void onFailure(Throwable caught) {
        RootPanel.get("content").getElement().setInnerText("");
        GWT.log("Error", caught);
        if (caught instanceof NotLoggedInException)
            RootPanel.get("content").add(new Label("You must login to access to this profile"));
        else
            RootPanel.get("content").add(new Label("Error : " + caught.getMessage()));
    }
    
    private native String getUsername() /*-{
    // Get a reference to the first customer in the JSON array from earlier
    return $wnd.username; 
  }-*/;
}
